package com.example.day002_kt_livedata;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.day002_kt_livedata.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    private MutableLiveData<String> data = new MutableLiveData<>();
    private Button btnMainRoute;
    private Button btnMainRoute1;
    private Button btnLiveDataText;
    private Button btnMain;


    //上一次按下返回键的事件   第一次为0
    long intex = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断按下的是返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //获取系统当前事件
            long l = System.currentTimeMillis();
            if (l - intex > 2000) {//不退出
                intex = l;//更新上一次的时间
                Toast.makeText(this, "再点就退出", Toast.LENGTH_SHORT).show();
            } else {//退出
                finish();
            }
            return true;//结束程序
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setLiveData(this);

        binding.views.start();



        data.observe(this, s -> {
            Log.i("111", "onCreate: " + s);
        });


        data.observeForever(new Observer<String>() {
            @Override
            public void onChanged(String s) {

            }
        });

        initView();
    }

    public void onClicks(View view) {
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            data.setValue("11");
        } else {
            data.postValue("22");
        }


    }

    private void initView() {
        btnMainRoute = (Button) findViewById(R.id.btn_main_route);
        btnMainRoute1 = (Button) findViewById(R.id.btn_main_route1);

        btnMainRoute.setOnClickListener(v -> {
            ARouter.getInstance().build("/homecenter/main")
                    .navigation();
        });

        btnMainRoute1.setOnClickListener(v -> {
            ARouter.getInstance().build("/test/main")
                    .withString("param1", "我是主页面")
                    .navigation();
        });

        btnMain = (Button) findViewById(R.id.btn_main);
        btnMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/home/main")
                        .navigation();
            }
        });
    }
}